﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebApiTest.Dto.Basic;
using WebApiTest.Dto.Utils;
using WebApiTest.Models;

namespace WebApiTest.Service.Office

{
    public class ObjectService
    {
        officeContext db = new officeContext();

        public TableRspDTO list4Table(TableReqDTO tableReqDTO)
        {
            long count = db.OfficeObject
                .Where(u => string.IsNullOrEmpty(tableReqDTO.parseQueryParam("objectName")) || u.ObjectName.Contains(tableReqDTO.parseQueryParam("objectName")))
                .Where(u => string.IsNullOrEmpty(tableReqDTO.parseQueryParam("typeId")) || u.TypeId.ToString().Equals(tableReqDTO.parseQueryParam("typeId")))
                .Where(u => u.IsDelete == 0)
                .Where(u => u.Number > 0)
                .LongCount();
            List<JObject> res = db.OfficeObject
                .Join(db.Type, p => p.TypeId, d => d.TypeId,
                (p, d) => new { key = p.ObjectId, objectId = p.ObjectId, typeId = p.TypeId,objectName = p.ObjectName, number = p.Number, gmtCreate = p.GmtCreate, gmtUpdate = p.GmtUpdate, isDelete = p.IsDelete })
                .Where(u => string.IsNullOrEmpty(tableReqDTO.parseQueryParam("objectName")) || u.objectName.Contains(tableReqDTO.parseQueryParam("objectName")))
                .Where(u => string.IsNullOrEmpty(tableReqDTO.parseQueryParam("typeId")) || u.typeId.ToString().Equals(tableReqDTO.parseQueryParam("typeId")))
                .Where(u => u.isDelete == 0)
                .Where(u => u.number > 0)
                .OrderByDescending(u => u.objectId)
                .Skip(tableReqDTO.getStart())
                .Take(tableReqDTO.pageSize)
                .ToList()
                .ConvertAll(r => JObject.FromObject(r));
            PagingDTO pagingDTO = new PagingDTO(
                tableReqDTO.pageNo,
                tableReqDTO.pageSize,
                count,
                res
                );
            return new TableRspDTO(pagingDTO);
        }

        public int add(OfficeObject officeObject)
        {
            officeObject.GmtCreate = DateTime.Now;
            officeObject.IsDelete = 0;
            db.OfficeObject.Add(officeObject);
            db.SaveChanges();
            return 1;
        }

        public int update(OfficeObject officeObject)
        {
            OfficeObject o = db.OfficeObject.Find(officeObject.ObjectId);
            o.ObjectName = officeObject.ObjectName;
            o.TypeId = officeObject.TypeId;
            o.Number = officeObject.Number;
            db.SaveChanges();
            return 1;
        }
        public int remove(string ids)
        {
            List<OfficeObject> list = new List<OfficeObject>();
            string[] idList = ids.Split(",");
            foreach (string id in idList)
            {
                OfficeObject officeObject = db.OfficeObject.Find(long.Parse(id));
                officeObject.IsDelete = 1;
                //list.Add(db.OfficeObject.Find(long.Parse(id)));
            }
            //db.OfficeObject.RemoveRange(list);
            //更新到数据库
            db.SaveChanges();
            return idList.Length;
        }
        public List<JObject> listAll()
        {
            return db.OfficeObject
                .Select(e => new { objectId = e.ObjectId, objectName = e.ObjectName, number = e.Number, typeId = e.TypeId, isDelete = e.IsDelete })
                .ToList()
                .ConvertAll(e => JObject.FromObject(e));
        }

    }
}
